﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Diagnostics;

namespace Kaixin.Tools
{
    public class Network
    {
        #region Member
        private string _UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)";
        private CookieContainer _cc = new CookieContainer();
        #endregion

        
        public string PostData(string url, byte[] data)
        {
            HttpWebRequest loHttp = null;
            HttpWebResponse loWebResponse = null;
            StreamReader loResponseStream = null;
            string response = "";

            try
            {
                loHttp = (HttpWebRequest)WebRequest.Create(url);
                loHttp.AllowAutoRedirect = false;
                loHttp.Method = "POST";
                loHttp.UserAgent = _UserAgent;
                loHttp.KeepAlive = true;
                loHttp.Referer = Consts.RootURL + "/";
                loHttp.ContentType = "application/x-www-form-urlencoded";
                loHttp.Headers.Add("Accept-Encoding", "gzip,deflate");

                loHttp.CookieContainer = _cc;

                if (data != null)
                {
                    loHttp.ContentLength = data.Length;
                    Stream loPostData = loHttp.GetRequestStream();
                    loPostData.Write(data, 0, data.Length);
                    loPostData.Close();
                }

                loWebResponse = (HttpWebResponse)loHttp.GetResponse();
                Stream stream = loWebResponse.GetResponseStream();
                GZipStream zstream = new GZipStream(stream, CompressionMode.Decompress);
                loResponseStream = new StreamReader(zstream, Encoding.UTF8);
                response = loResponseStream.ReadToEnd();

                return response;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
                return "";
            }
            finally
            {
                if (loWebResponse != null)
                    loWebResponse.Close();
                if (loResponseStream != null)
                    loResponseStream.Close();
            }
        }


        public string CallUrl(string url)
        {
            HttpWebRequest loHttp = null;
            HttpWebResponse loWebResponse = null;
            StreamReader loResponseStream = null;
            string response = "";

            try
            {
                loHttp = (HttpWebRequest)WebRequest.Create(url);
                loHttp.Method = "GET";
                loHttp.UserAgent = _UserAgent;
                loHttp.KeepAlive = true;
                loHttp.Headers.Add("Accept-Encoding", "gzip,deflate");

                loHttp.CookieContainer = _cc;

                loWebResponse = (HttpWebResponse)loHttp.GetResponse();
                Stream stream = loWebResponse.GetResponseStream();
                GZipStream zstream = new GZipStream(stream, CompressionMode.Decompress);
                loResponseStream = new StreamReader(zstream, Encoding.UTF8);
                response = loResponseStream.ReadToEnd();

                return response;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
                return "";
            }
            finally
            {
                if (loWebResponse != null)
                    loWebResponse.Close();
                if (loResponseStream != null)
                    loResponseStream.Close();
            }
        }

        public byte[] GetData(string url)
        {
            HttpWebRequest loHttp = null;
            HttpWebResponse loWebResponse = null;

            try
            {
                loHttp = (HttpWebRequest)WebRequest.Create(url);
                loHttp.Method = "GET";
                loHttp.UserAgent = _UserAgent;
                loHttp.KeepAlive = true;
                loHttp.Headers.Add("Accept-Encoding", "gzip,deflate");

                loHttp.CookieContainer = _cc;

                loWebResponse = (HttpWebResponse)loHttp.GetResponse();
                Stream stream = loWebResponse.GetResponseStream();
                GZipStream zstream = new GZipStream(stream, CompressionMode.Decompress);

                List<byte> data = new List<byte>();

                if (zstream != null)
                {
                    int b = zstream.ReadByte();
                    while (b != -1)
                    {
                        data.Add((byte)b);
                        b = zstream.ReadByte();
                    }
                }

                return data.ToArray();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
                return null;
            }
            finally
            {
                if (loWebResponse != null)
                    loWebResponse.Close();
            }

        }

    }
}
